<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <h1>过滤器</h1>
        <p>{{title}}</p>
        <p>msg -- {{msg | upper}}</p>
        <p>msg2-- {{msg | lower}}</p>
        <p>num-- {{num | toFixed(3)}}</p>
        <p>grade--{{grade | toGrade}}</p>
        <p>time -- {{time | timeFormat}}</p>
    </div>
</body>
<script>

    // 全局过滤器
    Vue.filter("upper", value => {
        if (!value) {
            return ""
        }
        return value.toUpperCase()
    })
    var vm = new Vue({
        // 英文小写过滤
        filters: {/* 局部过滤器  filters -- 复数*/
            lower(value) {
                if (!value) return "";
                return value.toLowerCase()
            },
            // 数字保留两位小数过滤
            toFixed(value, n) {
                if (!value) return "";
                n = n || 2;
                return value.toFixed(n)
            },
            // 成绩过滤
            toGrade(value) {
                if (!value) return "";
                value = parseInt(value / 10);
                switch (value) {
                    case 10:
                        return "A";
                        break
                    case 9:
                        return "A";
                        break
                    case 8:
                        return "B";
                        break;
                    case 7:
                        return "C";
                        break;
                    case 6:
                        return "D";
                        break;
                    default:
                        return "E"
                        break;
                }
            },
            // 时间格式化过滤
            timeFormat(value, str) {
                if (!value) return "";
                str = str || '-'
                var time = new Date(value)
                var year = time.getFullYear()
                var month = time.getMonth() + 1
                var day = time.getDate()
                var hour = time.getHours()
                var min = time.getMinutes()
                var sec = time.getSeconds()
                return `${year}${str}${month}${str}${day} ${hour}:${min}:${sec}`
            },
            // 货币金额过滤


        },
        data: {
            title: "v-on 事件绑定",
            msg: "wh2105-DayDayUp",
            num: 100.525251,
            grade: 96,
            time: new Date(),
            money: 5200231234.78966,
        }
    }).$mount("#app")
</script>

</html>